package com.hulu.yec.teach.examplan.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hulu.yec.common.exception.DaoRuntimeException;
import com.hulu.yec.teach.examplan.common.util;
import com.hulu.yec.teach.examplan.entity.ExamPlanEntity;

public class BackListDao {
	final static String DRIVER = "com.mysql.jdbc.Driver";
	final static String URL = "jdbc:mysql://192.168.12.252:3306/exam";
	final static String USERNAME = "root";
	final static String PASSWORD = "root";

	/**
	 * 加载驱动连接数据库
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 */
	private static Connection getConnection() throws ClassNotFoundException {
		Connection connection = null;

		try {
			Class.forName(DRIVER);
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (SQLException e) {
			throw new DaoRuntimeException("连接数据库失败");
		}
		return connection;
	}

	/**
	 * 试卷回显
	 * 
	 * @return
	 */
	public static List<ExamPlanEntity> PaperList() {
		String sql = "SELECT id, name FROM qes_exam_paper";
		return util.BackList(sql);

	}

	/**
	 * 班级的回显
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static List<ExamPlanEntity> ClassList() {
		String sql = "SELECT id, name FROM sys_course";
		return util.BackList(sql);

	}

	/**
	 * 回显操作者
	 * 
	 * @param userId
	 * @return
	 */
	public static List<ExamPlanEntity> MangerList(int userId) {
		String sql = "SELECT id, name FROM sys_user WHERE id=" + userId;
		return util.BackList(sql);
	}

	/**
	 * 获取用户ID
	 * 
	 * @param id
	 * @return
	 */
	public static List<ExamPlanEntity> UserId(int id) {
		String sql = "SELECT operate_user_id FROM exam_plan WHERE id=" + id;
		return util.userId(sql);
	}

	/**
	 * 关闭资源
	 * 
	 * @param parameters
	 */
	private static void close(Object... parameters) {
		try {
			if (parameters != null && parameters.length > 0) {
				for (Object obj : parameters) {
					if (obj instanceof ResultSet) {
						((ResultSet) obj).close();
					}

					if (obj instanceof PreparedStatement) {
						((PreparedStatement) obj).close();
					}

					if (obj instanceof Connection) {
						Connection conn = (Connection) obj;
						if (conn != null && !conn.isClosed()) {
							conn.close();
							conn = null; // 销毁内存连接对象
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
